﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.Protal.Models;
using Microhard.Protal.Resource;
using Microhard.Protal.Interfaces;
using Microhard.Core;
using FluentValidation.Results;

namespace Microhard.Protal.Validators
{
    public class SiteInfoModelValidator : AbstractValidator<SiteInfoModel>
    {
        public SiteInfoModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Key).NotEmpty().WithMessage(Msg.SiteInfo_NeedKey).When(p => p.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Key).Length(1, 50).WithMessage(Msg.MaxKey).When(p => p.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.LinkValue).Length(1, 50).WithMessage(Msg.MaxLinkValue).When(p => p.OperationStatus == Core.DBUtility.OperationStatus.Add);
                Custom(u =>
                {
                    ISiteInfo isi = IoC.Resolve<ISiteInfo>();
                    if (isi.IsDuplicateKey(u))
                    {
                        return new ValidationFailure("Key", Msg.DuplicateSKU);
                    }
                    return null;
                });

            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.Key).NotEmpty().WithMessage(Msg.SiteInfo_NeedKey);
                RuleFor(u => u.Key).Length(1, 50).WithMessage(Msg.MaxKey);
                RuleFor(u => u.LinkValue).Length(1, 50).WithMessage(Msg.MaxLinkValue);
                Custom(u =>
                {
                    ISiteInfo isi = IoC.Resolve<ISiteInfo>();
                    if (isi.IsDuplicateKey(u))
                    {
                        return new ValidationFailure("Key", Msg.DuplicateSKU);
                    }
                    return null;
                });
            });

        }
    }
}
